0

How to check the value of "newOk":(true/false), which comes in the response body from the post request?

httpAddr = 'https://<link>'
client = WebClient()
client.Headers.Add("Content-Type","application/json")
client.Headers.Add("Authorization","Basic <code>")
client.Encoding = System.Text.Encoding.UTF8;
webRequest = WebRequest.Create(httpAddr);
reply = client.UploadString(httpAddr, body.ToString())
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • You should parse your `reply` string to a json object, so you can do something like: `var newOk = jsonObject["newOkay"]`. Also the `WebClient` is not recommended: https://stackoverflow.com/a/4015346/12473121 – radoslawik Oct 27 '22 at 12:48
  • Provide response also – Pradeep Kumar Oct 27 '22 at 14:08

2 Answers2

0

To read the body of a response, you can use the method GetWebResponse (Read: https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.getwebresponse?view=net-6.0) on your WebClient object instance.

Then, in the WebReponse class you can use the method GetResponseStream to read the body of the response of that request.

Depending on how the body response is formatted (html, json, xml), you'll need to parse it with the appropriate .net class.

Rivo R.
  • 351
  • 2
  • 8
0

I get it:

webRequest = WebRequest.Create(httpAddr);
try:
    reply = client.UploadString(httpAddr, body.ToString())
    if reply.ToString() == '{"newOk":true,"redirectOk":false,"rowCount":0,"datname":null}':
        log 'ID успешно изменен'
    if reply.ToString() == '{"newOk":false,"redirectOk":false,"rowCount":0,"datname":null}':
        log "Такой ID уже существует", 1  
        raise MacroError('Такой ID уже существует')

except e as System.Net.WebException:
    resp = StreamReader((e.Response as HttpWebResponse).GetResponseStream()).ReadToEnd()
    log "Произошла ошибка!" + e.Message.ToString(), 1
    log "Произошла ошибка!" + resp.ToString(), 1    
    raise MacroError("Произошла ошибка! Ошибка: ${e.Message}. Параметры запроса: body = ${body.ToString()}; httpAddr = ${httpAddr}; Ответ сервера = ${resp.ToString()}")
ensure:
    client.Dispose()
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '22 at 18:22