on 2023 Nov 17 7:37 AM
Hello everyone,
As you can see in the links below the CLR Module Import is No Longer Supported. I am trying to use the new way using Using RestClient but I am getting an error.
FUNCION: UpdateC4CQuote_new: Error: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Can some write an example of how to send a Quote.GetQuoteInXML() using Using RestClient?
old code
import Systemfrom System import Uri, Convert
from System.Net.Http import HttpClient, HttpRequestMessage, HttpMethod, StringContent
from System.Text.Encoding import UTF8
try:if Quote.OrderStatus.Id != 38 and Quote.OrderStatus.Id != 2:
#Trace.Write("gs_UpdateC4CQuote")
client = HttpClient() request = HttpRequestMessage(HttpMethod.Post, "xxxxxxxxxxxxxxxxxxxxxxxxx")
request.Headers.Add("Authorization", "Basic xxxxxxxxxxxxxxxxxx")
content = Quote.GetQuoteInXML()
request.Content = StringContent(content)
response = client.SendAsync(request)
#response.EnsureSuccessStatusCode()
#Trace.Write(response.Content.ReadAsStringAsync())
Log.Write("UpdateC4CQuote response - " + str(response))
except Exception, e:
Log.Error("FUNCION: UpdateC4CQuote: Error: " + str(e))
new code
this box doesn't work
try:
if Quote.OrderStatus.Id != 38 and Quote.OrderStatus.Id != 2:
url= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
accessToken = RestClient.GetBasicAuthenticationHeader("xxxxxxxx","xxxxxxxxxxxxx")
headers = {"Authorization": "Basic " + str(accessToken) }
Log.Write("FUNCION UpdateC4CQuote_new - headers: "+str(headers))
document = Quote.GetQuoteInXML()
contenido = StringContent(document, Encodings.UTF7, 'text/xml')
formContent = MultipartFormDataContent()
formContent.Add(contenido,"application/xml")
c = RestClient.Post(url, headers, formContent)
Log.Write("FUNCION UpdateC4CQuote_new - end: ")
except Exception, e:
Log.Error("FUNCION: UpdateC4CQuote_new: Error: " + str(e))
Hi,
You should explicitly define the values of the Accept and Content-Type fields in the headers like this:
...
requestBody = Quote.GetQuoteInXML()
requestHeaders = {
'Authorization': 'Basic ' + accessToken,
'Accept': 'application/xml',
'Content-Type': 'text/xml'
}
response = RestClient.Post(url, requestBody, requestHeaders)
...
The Content-Type header describes the format of the payload (parameters) that is sent (application/json, application/xml, etc.), while the Accept header describes the format of the response that is expected.
"Unexpected character encountered while parsing value: <. Path '', line 0, position 0." message means that RestClient cannot parse the payload response you received. The default value for Accept and Content-Type headers for RestClient is application/json. If you expect XML data for the response payload, you should explicitly define a value of the Accept header as application/xml (or text/xml).
Regards,
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
4 | |
4 | |
3 | |
3 | |
2 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.