POST request with XML payload via reqwest

I'm having trouble making a POST request with an XML body via the reqwest crate. For now, I'm just passing a formatted String to the body method, kind of like this:

let body = format!(r#"<?xml version="1.0" encoding="UTF-8"?>
      <Example>
            <DummyData>Foo</DummyData>
            <DummyInt>{}</DummyInt>
      </Example>
"#, 5)

let client = blocking::Client::builder()
      .user_agent(APP_USER_AGENT)
      .build()?

let res = client
      .post(url)
      .body(body) // passing my String to the body method, problem is here
      .send()
      .unwrap();

I'm getting a status code of 415 with an "Unsupported Media Type" error message which occurs when the payload format is unsupported by the server. I know for sure the issue is with my code because I can call this same API with Node.js and include a string template literal and it works fine.

Have you tried settings the content type to application/xml ? With the header method on the RequestBuilder. (and this constant).

2 Likes

Just figured that out as you were typing your response :slightly_smiling_face:. Working code ended up looking like:

let res = client
      .post(url)
      .header("Content-Type", "application/xml") // this fixed it
      .body(body)
      .send()
      .unwrap();
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.