cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with RestClient sending a xml

pepeverdu
Explorer
0 Kudos
419

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 System

from 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))







As of 2311: CLR Module Import No Longer Supported

Alternatives to CLR Module Import

Accepted Solutions (1)

Accepted Solutions (1)

silic
Employee
Employee

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

Answers (0)