-2

How can I convert the code below to vb.net/c#?

I've tried the various examples found online but cant seem to get it going?

curl -H "Accept: application/json+v6" -H "x-api-key: <api_key>" \https://some.thing.uk/fred/prices\?productcode=ZZ99ABC

I expect it to return some results but I keep getting Forbidden (403).

Dr T
  • 504
  • 1
  • 7
  • 20
  • SO is not a code conversion service. If you want to write equivalent code in a different language then you first need to understand what the original code does. You then need to learn how to do those things in the new language. You then need to write what you think is the correct code in the new language. If the code you write doesn't work, post THAT code, explain what it is supposed to do and how what it actually does differs from that. The original code could be posted as reference. – jmcilhinney Jul 09 '19 at 01:57
  • This is a curl command to make a GET request to an URL. You can do the same thing in C# using HttpClient class or using RestSharp libraries. [See this](https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c) and [this](http://restsharp.org/) – Chetan Jul 09 '19 at 01:57
  • You can use both WebRequest (add the Headers with [HttpWebRequest.Headers.Add()](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.headers)) and HttpClient (add the Headers with [HttpClient.DefaultRequestHeaders.Add()](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultrequestheaders)). – Jimi Jul 09 '19 at 02:18

1 Answers1

1

I think RestSharp will be your best bet. It will work for C# or vb.net. I am fairly new to the use of RestSharp but it has worked well for me. You might have to tweak the code below a little bit since I can't test this answer without an api key but it should get you started. You will also need to install RestSharp via Nuget first and then import it into your class.

        Dim key As String = 'your api key'
        Dim client As New RestClient("https://some.thing.uk/fred/prices")
        Dim pagesrequest = New RestRequest("\?productcode=ZZ99ABC" & "&x-api-key:" & key, Method.GET)
        Dim response As IRestResponse = client.Execute(pagesrequest)
        Dim textresponse As String = response.Content
        'Display the response so you can check it.
        textbox1.text=textresponse

Also, "\?productcode=ZZ99ABC" doesn't look right to me. You might want to try it withoug the "\".

Jamie
  • 555
  • 3
  • 14