Results 1 to 3 of 3

Thread: [RESOLVED] CURL in VB.net (API)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Resolved [RESOLVED] CURL in VB.net (API)

    Does anyone have a working sample of CURL to VB.net? I have exhausted every example I can find and all of them return NULL search results. I can run the CURL from a command prompt and it returns everything expected. I just can't find a way to get this to work from VB:

    This CURL works perfectly from a command prompt:

    Code:
    curl -X POST "https://api.mouser.com/api/v1/search/keyword?apiKey=YOURSECRETKEY" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"SearchByKeywordRequest\": {    \"keyword\": \"LMK105BJ224KV-F\",    \"records\": 0,    \"startingRecord\": 0,    \"searchOptions\": \"string\",    \"searchWithYourSignUpLanguage\": \"string\"  }}"
    My code attempt that returns the header information, but the SearchResult is NULL:

    Imports Newtonsoft.Json

    Code:
            ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
            Dim Mfr As String = "string"
            'Dim dictData As New Dictionary(Of String, String)
            'dictData.Add("keyword", Mfr)
            'dictData.Add("records", 0)
            'dictData.Add("startingrecord", 0)
            'Dim DictStr As String = JsonConvert.SerializeObject(dictData, Formatting.None)
            Dim senddata As String = "{  \""SearchByKeywordRequest\"": {    \""keyword\"": \""LMK105BJ224KV-F\"",    \""records\"": 0,    \""startingRecord\"": 0,    \""searchOptions\"": \""string\"",    \""searchWithYourSignUpLanguage\"": \""string\""  }}"
            Dim web As New Net.WebClient
            web.Headers.Add(Net.HttpRequestHeader.Accept, "application/json")
            web.Headers.Add(Net.HttpRequestHeader.ContentType, "application/json")
            Dim response = web.UploadString("https://api.mouser.com/api/v1/search/keyword?apiKey=YOURSECRETKEY", "POST", senddata)
    
            Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(response)

    (I am reposting the code here without a wrapper because it keeps removing my '\' escapes when I wrap it in VB.net highlight)



    This is just one example of about half a dozen I have tried, all with the same result. I believe it must have something to do with formatting the 'senddata' string correctly but I'm not sure.
    Last edited by Fedaykin; Oct 28th, 2020 at 04:52 PM.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: CURL in VB.net (API)

    Yes, it is your senddata string... remove the \ before each of the double quotes ... that's making it invalid json. It's needed from the command prompt to escape the quotes that follow it... but in VB, the "" is "auto" escaped...
    Code:
    Dim senddata As String = "{  ""SearchByKeywordRequest"": {    ""keyword"": ""LMK105BJ224KV-F"",    ""records"": 0,    ""startingRecord"": 0,   ""searchOptions"": ""string"",    ""searchWithYourSignUpLanguage"": ""string""  }}"
    Personally, I'd just clean it up with single quote marks and call it a day:
    Code:
    Dim senddata As String = "{'SearchByKeywordRequest': {'keyword':'LMK105BJ224KV-F', 'records': 0, 'startingRecord': 0, 'searchOptions':'string', 'searchWithYourSignUpLanguage':'string'  }}"
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: CURL in VB.net (API)

    Grrrrr, yes in that example. In all my previous tries I had a trailing comma in the string! Found it right when I posted! Sorry about that.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width