I am trying to send Mailgun POST /messages API request in Android using retrofit library.
Following is retrofit request:
HTTP POST https://api:key-xxx/v3/sandboxxxx/messages
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 148
from=Excited+User+%3Cmailgun%40sandboxxxxmailgun.org%3E&to=vir.jain%40gmail.com&subject=Hello&text=Testing+awesomeness
Response:
HTTP 401 https://api:key-xxx/v3/sandboxxxx.mailgun.org/messages (1966ms)
Server: nginx/1.7.9
Date: Mon, 15 Jun 2015 10:00:37 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 9
Connection: keep-alive
WWW-Authenticate: Basic realm="MG API"
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1434362440805
OkHttp-Received-Millis: 1434362441086
Forbidden
When I try this same request on Postman, it works perfectly & email is sent properly. But when sending using retrofit library from Android fails with Forbidden.
My Retrofit API: @FormUrlEncoded @POST("/messages") public void sendEmail(@Field("from") String from, @Field("to") String to, @Field("subject") String subject, @Field("text") String text, RestCallback objectRestCallback);
Base URL is: https://api:key-xxx/v3/sandboxxxsandboxxxx.mailgun.org
Everything is form URL encoded. But I'm really not sure what going wrong. Can anyone please help me out whats wrong with above API?
Thank you, Mahavir
I added following authorization header with Base64 encoded string of API key to the request, it started working.
request.addHeader("Authorization", "Basic "+getBase64String("api:key-xxxYOUR-API-KEY>"));
@Selvin: Thanks so much for sending wiki link :).