0

i'm trying to get a response from an httpwebrequest using get method and content type json.. but i'm getting Cannot send a content-body with this verb-type

here is my code:

Dim objRequest As HttpWebRequest = WebRequest.Create(url)
Dim reqBytes As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(strPost)
objRequest.Method = "GET"
objRequest.Timeout = "15000"
objRequest.ContentLength = reqBytes.Length
objRequest.ContentType = "application/json; charset=utf-8" 

Try
   myWriter = objRequest.GetRequestStream()
   myWriter.Write(reqBytes, 0, reqBytes.Length)

Catch e As Exception
   writetotext(e.toString)
End Try

am i missing something here?

1 Answer 1

3

HTTP GET cannot have a message body. Data gets typically passed through the URI path and query string and not through message body for GET requests. For POST, PUT, etc, you should be able to do what you are trying to do in the code above.

Sign up to request clarification or add additional context in comments.

2 Comments

ok so i removed this objRequest.ContentType = "application/json; charset=utf-8" but i need to get the response of the request as json is there anything i have to add?
Try objRequest.Accept="application\json". But the main problem is you cannot write into the request stream. GET cannot have a message body. The payload you have in the form of strPost can be either posted using "POST" method or passed in, in the form of query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.