Nutshell API authentication with C# - c#

I'm trying to use C# to connect to the Nutshell API.
I'm unsure where the method name goes. Is it on the URL or as part of the JSON? I have tried both ways without success. I know my email is valid as I have tried the same method on the nutshell website
Here is my code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.nutshell.com/v1/json/");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"method\": \"getApiForUsername\", \"user\":\"myemail#email.com.au\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
What am I doing wrong?

You want your response body to look like this:
{"jsonrpc":"2.0","method":"getUser","params":[],"id": "apeye"}
For this url:
https://app01.nutshell.com/api/v1/json
TIP: use a JSON serializer to create your JSON strings

Related

C#: Huggingface API - Text to Speech

I want to use a speech to text API in C# from huggingface(https://huggingface.co/facebook/fastspeech2-en-ljspeech) but I'm facing some difficulties.
I tried the following :
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-
inference.huggingface.co/models/facebook/fastspeech2-en-ljspeech");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{'text': 'My name is Teven and I am'}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
byte[] byteArray = Encoding.UTF8.GetBytes(result); //Not sure about this line
File.WriteAllBytes(#"C:\Users\...\test.flaC", byteArray);
}
I'm not sure how to use it, I got as an output the test.flaC audio file, but it does not work. I know that C# have an internal Text2Speech API, but I want to use this one because it has better features.

How to send json data in POST request using C# [duplicate]

This question already has answers here:
How to post JSON to a server using C#?
(15 answers)
Closed 5 years ago.
I want to send json data in POST request using C#.
I have tried few ways but facing lot of issues . I need to request using request body as raw json from string and json data from json file.
How can i send request using these two data forms.
Ex: For authentication request body in json --> {"Username":"myusername","Password":"pass"}
For other APIs request body should retrieved from external json file.
You can use either HttpClient or RestSharp. Since I do not know what your code is, here is an example using HttpClient:
using (var client = new HttpClient())
{
// This would be the like http://www.uber.com
client.BaseAddress = new Uri("Base Address/URL Address");
// serialize your json using newtonsoft json serializer then add it to the StringContent
var content = new StringContent(YourJson, Encoding.UTF8, "application/json")
// method address would be like api/callUber:SomePort for example
var result = await client.PostAsync("Method Address", content);
string resultContent = await result.Content.ReadAsStringAsync();
}
You can do it with HttpWebRequest:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://yourUrl");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "pass"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
This works for me.
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new
StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
Username = "myusername",
Password = "password"
});
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

(C#) Google Firebase HTTP post request to read/write data, returned 405 : Method not allowed.

I am looking to modify the Firebase realtime database from my web-app using an HTTP post request.
I have written the post request, but it seems to return a 405 error. Any good way around this?
Code below
var httpWebRequest = (HttpWebRequest)WebRequest.Create("*DatabaseURL*");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json2 = "{\"user\":\"test\"," +
"\"password\":\"bla\"}";
streamWriter.Write(json2);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Ended up using a custom SDK called FirebaseDatabase. Found here.
https://github.com/step-up-labs/firebase-database-dotnet

How to send JSON data as parameter from C# to Jersey RESTful Service

I have a C# Console program. I just want to send JSON data to a POST RESTful service. Which approach should I follow?
#Path("/SetInfo")
public class SetInfo {
#POST
#Produces({ MediaType.APPLICATION_JSON })
#Consumes({ MediaType.APPLICATION_JSON })
public String AuthMySQL(String json) {
System.out.println("The JAX-RS runtime automatically stored my JSON request data: " + json);
return "";
}
I solved the problem.........
C# code......for sending JSON Data to webservice (post).....
var webAddr = "http://localhost:8080/TestWebservice/rest/SetInfo";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"Name\":\"MR.X\",\"ID\":\"AH1J4\"}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.Write(result);
}

Sending an HTTP PUT request with a parameter in C#

I have a tika server open on my PC, and I need to send it a request with a string parameter which is a path for a file I want tika to process. The code I have so far is:
private void getFromServer(string fileName)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9998/rmeta");
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "PUT";
httpWebRequest.KeepAlive = true;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string postData = #"C:\Users\######\Downloads\elasticsearch\elasticsearch-1.7.1.zip";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
string json = postData;
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
console.write(responseText);
}
}
The thing is, the server gets the request, but it returns the string I sent it instead of the contents of the file in question, and I know it can work because I sent it the same request using a cURL command and it worked. Any ideas what I'm doing wrong?
PS the hashtags are instead of my username

Categories