0

I need to post some data to server using XML.

The XML body of my request is

        requestBody = '<?xml version="1.0" encoding="UTF-8"?><xmlreq><record name="direct" status="new"><field name="company-name">Test2</field></record></xmlreq>';

I use this request

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        req.setEndpoint(URL);
        req.setMethod('POST');
        req.setBody(body);
        req.setTimeout(120000);
        req.setCompressed(true);
        Http http = new Http();
        try {
            res = http.send(req);
            System.debug('Callout OK: ' + res.getBody());
        } catch (System.CalloutException e) {
            System.debug('Callout error: ' + e);
            System.debug(res.toString());
        }

In response, I get a phrase - Unable to read the input data. Error on my part or on the server-side? I think that there is an error in my XML or in a way of its transfers to the server.

Thank you

1
  • Are you sure the target system accepts a compressed body? Aug 3, 2021 at 15:56

1 Answer 1

0

That XML is fine, though I would generally recommend you use Dom.Document to generate your XML. However, you did not specify a Content-Type, which may cause your application to not understand the data you're passing in. Try:

req.setHeader('Content-Type', 'application/xml');
1
  • Thank you for your answer but this didn't solve my issue (i got the same issue). Maybe we need some other approach? Aug 3, 2021 at 7:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .