httpPost to httpClient

Hi,

I am trying to convert from system.net.httpPost to system.net.httpClient. Can anyone help me?

Original:

system.net.httpPost(postUrl,"application/json","{\"value1\":\"" + str(value1) + "\"}")

Change to:

postResult = clientPost.post(postUrl,'application/json',{"machine_number":"string"})

Error:

{"detail":[{"type":"missing","loc":["body"],"msg":"Field required","input":null}]}

Thank you.

Use explicit parameter names:
data={your payload}

1 Like

What pascal said. For your example I believe it should be:

postResult = clientPost.post(url=postUrl,
                             headers={'Content-Type':'application/json'},
                             data={"machine_number":"string"}
                             )

Note that setting a Content-Type: application/json header is not required if you're passing a dictionary to data - it'll automatically be sent with a JSON content type.

1 Like