none
HttpWebRequest : Is it possible to change JSON body to HTML to post data

    Question

  • Hi, I need help please.

    I'd like to know whether it's possible to change JSON body to the HTML to post data.

    I use the below JSON body to post data.

    'Set request body.
               
                Dim body As String = "{""ads"":[{""title"":""…" + txtbx + "…"",""description"":""<p>" + Form3.txtbx_descript.Text.Replace("""", "\""") + "</p>"",""phone"":""0799654128"",""categoryId"":9250,""categoryName"":""Computer Operators"",""parentCategory"":8,""location"":{""latitude"":-33.8659683,""longitude"":18.534443499999952,""radius"":2000},""categoryAttributes"":[],""imageUrls"":[""https://i.ebayimg.com/00/s/NjAwWDgwMA==/z/~qIAAOSwU8hY75kG/$_1.PNG?set_id=8800005007""]}]}"
    


    Monday, April 17, 2017 12:35 PM

All replies

  • Typically you the JSON data is the body of the HTML Post request.  You'll need to refer to the documentation for the specific API you are using, but you'll normally just use a HttpClient to post the JSON to a particular URL; you may need to specify some headers with info such as your API key.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 17, 2017 1:35 PM
    Moderator
  • Hi, I need help please.

    I'd like to know whether it's possible to change JSON body to the HTML to post data.

    I use the below JSON body to post data.

    'Set request body.
               
                Dim body As String = "{""ads"":[{""title"":""…" + txtbx + "…"",""description"":""<p>" + Form3.txtbx_descript.Text.Replace("""", "\""") + "</p>"",""phone"":""0799654128"",""categoryId"":9250,""categoryName"":""Computer Operators"",""parentCategory"":8,""location"":{""latitude"":-33.8659683,""longitude"":18.534443499999952,""radius"":2000},""categoryAttributes"":[],""imageUrls"":[""https://i.ebayimg.com/00/s/NjAwWDgwMA==/z/~qIAAOSwU8hY75kG/$_1.PNG?set_id=8800005007""]}]}"


    LMC,

    It looks like you're trying to write the JSON on the fly?

    There are syntax errors (use the ampersand to concatenate strings, not the plus sign) and if "txtbx" is a TextBox then you want to use the .Text property.

    All that said though, constructing JSON on the fly isn't the best way to do it. You can build a class to do that then serialize one or more instances to JSON.

    This is a great tool to help and it's free:

    http://www.newtonsoft.com/json/help/html/ReadingWritingJSON.htm

    Where the HTML comes in, I'm not sure, but you need to start with valid JSON.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Monday, April 17, 2017 6:06 PM
  • Hi LetMecode,

    What's your HTML, Form or other? Usually we use HttpWebRequest like this below.

    Dim strPost As String = "Username=1&Password=1"
    Dim url As String = "http://localhost:50555/WebSite1/DummyLogin.aspx"
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    request.ContentType = "text/html"
    request.Method = "POST"
    

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 18, 2017 8:35 AM
    Moderator