1

I am trying to implement the Rest HTTP API of FCM batch requests, which is found here. This is the relevant piece of code I am trying to convert with reqwest:

curl --data-binary @batch_request.txt -H 'Content-Type: multipart/mixed; boundary="subrequest_boundary"' -H 'Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA' https://fcm.googleapis.com/batch

I got some code which returns a 400 bad request error. I am not sure what I am doing wrong. This is the code I currently have:

let mut to_send = "".to_string();

for message in messages {
    let json = serde_json::to_string(&message).unwrap();
    let request = format!(r#"
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary

POST /v1/projects/{}/messages:send
Content-Type: application/json
accept: application/json

{json}
"#, self.project_id);

    to_send += &request;
}

let resp = self
    .inner
    .post("https://fcm.googleapis.com/batch")
    .timeout(self.timeout)
    .bearer_auth(tok.token().unwrap())
    .header(CONTENT_TYPE, HeaderValue::from_static("multipart/mixed; boundary=\"subrequest_boundary\""))
    .body(to_send)
    .send()
    .await
    .map_err(|_| Error::Timeout)?;

Any idea what I am doing wrong?

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • Try testing the request with CURL/Postman to check whether your formatted JSON is valid or not (according to the request handler). This rust code seems fine. – Jishan Shaikh May 04 '23 at 11:38

1 Answers1

1

Turns out I only had to add --subrequest_boundary-- at the very end of the request

J. Doe
  • 12,159
  • 9
  • 60
  • 114