1

i am trying to post json request as following.

I am getting an error on last line. Error message shown at last line.

Dim myReq As HttpWebRequest
Dim myResp1 As HttpWebResponse

myReq = HttpWebRequest.Create("https://pro.mastersindia.co/oauth/access_token")
myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("username", Service_Provider_Username)
myReq.Headers.Add("password", Service_Provider_Password)
myReq.Headers.Add("client_id", client_id)
myReq.Headers.Add("client_secret", client_secret)
myReq.Headers.Add("grant_type", "password")

Dim mystream As Stream = myReq.GetRequestStream
myResp = myReq.GetResponse  ---- here i am getting an error  {"The remote server returned an error: (400) Bad Request."} 

if i send same json to same url via POSTMAN then i do get response. But via vb.net code, i am getting above error.

Am i missing something or doing anything wrong ?

Pls help.

Thanks

[ Following is working in postman, am i missing something in vb.net doing same] [enter image description here]1


enter image description here

Teknas
  • 541
  • 5
  • 17
  • What are you POSTing? I see that you are adding things to headers but not to the body for post. Also, where are you sending json objects as you would using postman? – shahkalpesh Sep 24 '19 at 09:50
  • this is just to get auth_token from service provider. – Teknas Sep 24 '19 at 09:53
  • i added postman screenshot, which works, but vb.net is not working. – Teknas Sep 24 '19 at 10:02
  • You hsould look at this it shows you how to send json over vb.net: https://stackoverflow.com/questions/7384534/how-to-post-a-json-to-a-specific-url-using-vb-net – Mederic Sep 24 '19 at 10:07
  • In Postman you are putting everything in the body, while in VB you are putting everything in headers. Refer to the site’s specifications for what you are supposed to do. – dsd7a7 Sep 24 '19 at 10:09
  • Look at [this](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contenttype?view=netframework-4.8) example to do a POST. – shahkalpesh Sep 24 '19 at 10:17
  • i added specification sent by service provider. i am new to json in vb.net, so pls help with the code. thanks – Teknas Sep 24 '19 at 10:21
  • @shahkalpesh i tried to understand the link which you sent. But it point to specifically content type property only. I could not resolve it. – Teknas Sep 24 '19 at 10:38
  • @dsd7a7, how do i put in body then in vb.net. I also updated my question with specification provided by service provider. Hopefully it can help this forum to understand and help me. – Teknas Sep 24 '19 at 10:40
  • You may need to do the following to set the body `ASCIIEncoding encoding = new ASCIIEncoding (); byte[] byte1 = encoding.GetBytes (postData); myReq.ContentLength = byte1.Length; Stream newStream = myReq.GetRequestStream (); newStream.Write (byte1, 0, byte1.Length);`. You may have to use UTF8Encoding instead of ASCIIEncoding & you will need to create a json string of the key/value you are adding. – shahkalpesh Sep 24 '19 at 10:51
  • See https://stackoverflow.com/questions/53565130/how-can-i-create-json-on-the-fly-in-an-easier-way – dsd7a7 Sep 24 '19 at 11:11

1 Answers1

3

Your POST body is empty, you are supposed to put those into body not headers.

There are multiple ways to do it, here is one example.

Imports System.Net
Imports System.Text
Imports Newtonsoft.Json

    Public Class JSON_Post
        Public Property username As String
        Public Property password As String
        Public Property client_id As String
        Public Property client_secret As String
        Public Property grant_type As String
    End Class


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myReq As HttpWebRequest = HttpWebRequest.Create("https://pro.mastersindia.co/oauth/access_token")
        myReq.Method = "POST"
        myReq.ContentType = "application/json"

        Dim NewData As New JSON_Post
        NewData.username = "Service_Provider_Username"
        NewData.password = "Service_Provider_Password"
        NewData.client_id = "client_id"
        NewData.client_secret = "client_secret"
        NewData.grant_type = "password"

        Dim PostString As String = JsonConvert.SerializeObject(NewData)
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(PostString)
        myReq.ContentLength = byteArray.Length

        Dim dataStream As Stream = myReq.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close() 'sends request

        Dim myResp As HttpWebResponse = myReq.GetResponse()

    End Sub

Your body then looks like:

{"username":"Service_Provider_Username","password":"Service_Provider_Password","client_id":"client_id","client_secret":"client_secret","grant_type":"password"}
CruleD
  • 1,153
  • 2
  • 7
  • 15