-
Oct 28th, 2020, 04:40 PM
#1
Thread Starter
Addicted Member
[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.
-
Oct 28th, 2020, 05:03 PM
#2
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
-
Oct 28th, 2020, 05:05 PM
#3
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|