C
C#2y ago
SWEETPONY

✅ api response 401

hey, I'm trying to use nlpcloud to translate text there is curl example:
curl "https://api.nlpcloud.io/v1/nllb-200-3-3b/translation" \
-H "Authorization: Token <token>" \
-H "Content-Type: application/json" \
-X POST -d '{
"text":"John Doe has been working for Microsoft in Seattle since 1999.",
"source":"en",
"target":"fr"
}'
curl "https://api.nlpcloud.io/v1/nllb-200-3-3b/translation" \
-H "Authorization: Token <token>" \
-H "Content-Type: application/json" \
-X POST -d '{
"text":"John Doe has been working for Microsoft in Seattle since 1999.",
"source":"en",
"target":"fr"
}'
I wrote following nlpclient:
public class NlpTranslationClient
{
private readonly string _apiKey;
private readonly Uri _uri = new("https://api.nlpcloud.io/v1/nllb-200-3-3b/translation");

public NlpTranslationClient(string apiKey)
=> _apiKey = apiKey;

public async Task Translate(
string text,
string source,
string target)
{
using var client = new HttpClient();

client.DefaultRequestHeaders.Add("Authorization", _apiKey);

var translationObject = new TranslationObj
{
Text = text,
Source = source,
Target = target
};

var payload = new StringContent(
translationObject.ToJson(),
Encoding.UTF8,
"application/json");

var res = await client.PostAsync(_uri, payload); // just for testing
}
}
public class NlpTranslationClient
{
private readonly string _apiKey;
private readonly Uri _uri = new("https://api.nlpcloud.io/v1/nllb-200-3-3b/translation");

public NlpTranslationClient(string apiKey)
=> _apiKey = apiKey;

public async Task Translate(
string text,
string source,
string target)
{
using var client = new HttpClient();

client.DefaultRequestHeaders.Add("Authorization", _apiKey);

var translationObject = new TranslationObj
{
Text = text,
Source = source,
Target = target
};

var payload = new StringContent(
translationObject.ToJson(),
Encoding.UTF8,
"application/json");

var res = await client.PostAsync(_uri, payload); // just for testing
}
}
everything should be good but I get 401 in res whats wrong with my code?
11 Replies
jcotton42
jcotton422y ago
client.DefaultRequestHeaders.Add("Authorization", _apiKey); does _apiKey have the "Token" prefix? or just the token?
SWEETPONY
SWEETPONYOP2y ago
no, just a set of numbers and letters ok, I will add "Token"
jcotton42
jcotton422y ago
well, looking at your curl example, the header value should be Token <token here> not just <token here>
SWEETPONY
SWEETPONYOP2y ago
now api returns only the status code 400 Smadge
jcotton42
jcotton422y ago
400 is Bad Request I would use the debugger, and see what payload is
SWEETPONY
SWEETPONYOP2y ago
maybe my model is a problem idk..
jcotton42
jcotton422y ago
that's my guess the casing might be incorrect
SWEETPONY
SWEETPONYOP2y ago
public class TranslationObj
{
[JsonPropertyName("text")]
public string? Text { get; set; }

[JsonPropertyName("source")]
public string? Source { get; set; }

[JsonPropertyName("target")]
public string? Target { get; set; }
}
public class TranslationObj
{
[JsonPropertyName("text")]
public string? Text { get; set; }

[JsonPropertyName("source")]
public string? Source { get; set; }

[JsonPropertyName("target")]
public string? Target { get; set; }
}
but it looks as in example case
jcotton42
jcotton422y ago
hm no that should be fine...
jcotton42
jcotton422y ago
I'd grab something like https://www.telerik.com/fiddler/fiddler-classic and compare the request made by curl, and by your app
Telerik.com
Fiddler Classic | Original Web Capturing Tool for Windows
The community-trusted Windows-only web capture tool that logs HTTP(s) network traffic. Use Fiddler Classic to capture requests and responses.
SWEETPONY
SWEETPONYOP2y ago
okay I will try, thanks the problem was here: source="eng_Latn", target="spa_Latn"
Want results from more Discord servers?
Add your server