I am trying to integrate the HTTP API from transfernow.net into my windows forms app, but I'm hitting a weird brick wall...
I have tested the API call in postman with my key and it works perfectly, but the below C# is returning a 403 forbidden, can any of you genius coders point out whether I'm got something flawed in my code that would cause this (I have also reached out to Transfernow but expect they will not be responding quickly)
static async Task<string> SendPostRequestAsync()
{
System.Net.ServicePointManager.SecurityProtocol System.Net.SecurityProtocolType.Tls12;
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.transfernow.net/v1/transfers"))
{
request.Headers.TryAddWithoutValidation("x-api-key", "xxxxxx"); //removed for security reasons but was the same as used in postman
request.Content = new StringContent("{\n \"langCode\": \"en\",\n \"toEmails\": [\"my.contact@provider.com\", \"my.other.contact@provider.com\"],\n \"files\": [{\n \"name\": \"file1.txt\",\n \"size\": 14587\n },{\n \"name\": \"music.mp3\",\n \"size\": 5496409\n }],\n \"message\": \"A brillant transfer content, thank you\",\n \"subject\": \"Text and Music\",\n \"validityStart\": \"2022-10-28T08:52:25.955Z\",\n \"validityEnd\": \"2022-11-01T08:52:25.955Z\",\n \"allowPreview\": true,\n \"maxDownloads\": 7\n }");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
// Return the response content as a string
return await response.Content.ReadAsStringAsync();
}
else
{
// Handle an error response here
MessageBox.Show("Error: " + response.StatusCode, "API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
}
}
Any help would be much appreciated.
I've tried converting the curl code from transfernow documentation using https://curl.olsh.me/ and even using postman to generate and neither helped get me passed this issue.
postman generated code as requested :
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.transfernow.net/v1/transfers");
request.Headers.Add("x-api-key", "xxx");
var content = new StringContent("{\r\n \"langCode\": \"en\",\r\n \"toEmails\": [\"ajhbell76@gmail.com\", \"my.other.contact@provider.com\"],\r\n \"files\": [{\r\n \"name\": \"P:\\\\events\\\\test\\\\export\\\\DRB_0221woodlands.jpg\",\r\n \"size\": 2843846 \r\n },{\r\n \"name\": \"P:\\\\events\\\\test\\\\export\\\\DRB_0222woodlands.jpg\",\r\n \"size\": 2843846 \r\n }],\r\n \"message\": \"A brillant transfer content, thank you\",\r\n \"subject\": \"Text and Music\",\r\n \"allowPreview\": true,\r\n \"maxDownloads\": 7\r\n }", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
HTTP code generated by postman :
POST /v1/transfers HTTP/1.1
Host: api.transfernow.net
Content-Type: application/json
x-api-key: xxxxxx
Content-Length: 517
{
"langCode": "en",
"toEmails": ["ajhbell76@gmail.com", "my.other.contact@provider.com"],
"files": [{
"name": "P:\\events\\test\\export\\DRB_0221woodlands.jpg",
"size": 2843846
},{
"name": "P:\\events\\test\\export\\DRB_0222woodlands.jpg",
"size": 2843846
}],
"message": "A brillant transfer content, thank you",
"subject": "Text and Music",
"allowPreview": true,
"maxDownloads": 7
}
Apparently they also want a user-agent
header. It would be nice if the documentation mentioned that. I signed up for a trial and only got it to work by setting a user-agent. You probably don't need this exact value, but its what Github CoPilot wrote for me:
request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/999.0.9999.999 Safari/537.36");
I've encountered this before with other APIs. When in doubt, set some kind of user-agent and see if it helps.