Sending HTTP Request as an application/x-www-form-urlencoded
In my previous HttpRequests, I have used Content-Type = application/json but I have a vendor that is requesting we send application/x-www-form-urlencoded. How would I go about formatting the Payload as x-www-form-urlencoded?
Thanks
Comments
To send an HTTP request using the Content-Type: application/x-www-form-urlencoded, you can use the %Net.HttpRequest class in ObjectScript. Below is an example of how to create and configure the request:
- Create an instance of
%Net.HttpRequest. - Set the required properties, such as
Server,Port, andHttps. - Set the
ContentTypeto"application/x-www-form-urlencoded". - Add form data to the request using the
InsertFormDatamethod. - Call the
Postmethod to send the request.
Here is an example ObjectScript code:
Set httpRequest = ##class(%Net.HttpRequest).%New()
Set httpRequest.Server = "your.server.com"
Set httpRequest.Port = 443
Set httpRequest.Https = 1
Set httpRequest.ContentType = "application/x-www-form-urlencoded"
Set httpRequest.ContentCharset = "utf-8"
// Add form data
Do httpRequest.InsertFormData("key1", "value1")
Do httpRequest.InsertFormData("key2", "value2")
// Send the POST request
Set sc = httpRequest.Post("/your/endpoint")
// Check the response
If sc $$$ISOK {
Write httpRequest.HttpResponse.Data.Read()
} Else {
Write "Error: ", sc
}
Make sure to replace placeholders like "your.server.com" and the endpoint with actual values. In the example, form key-value pairs are added with the InsertFormData method [1][2].
Sources:
Hi,
You can also create a Generic Message request and receive a Generic Message response.
Set url = Endpointurl_"/"_Param1_"/"_Param2_"/"_Param3
Set httpResponse= ##class(EnsLib.HTTP.GenericMessage).%New()
Set HTTPrequest = ##class(EnsLib.HTTP.GenericMessage).%New()
Do HTTPrequest.HTTPHeaders.SetAt("application/x-www-form-urlencoded", "content-type")
Do HTTPrequest.HTTPHeaders.SetAt("GET","HttpRequest")
Do HTTPrequest.HTTPHeaders.SetAt(url, "URL")
Set tSC = ..OnMessage(HTTPrequest, .httpResponse)
Set StatusCode = $P(httpResponse.HTTPHeaders.GetAt("StatusLine")," ",2)
Thanks,