Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

node.js - Convert Paypal cURL command to Axios request

Following Paypal cURL command works as it supposed to be but unfortunately I couldn't convert to axios code to work same. Paypal Documentation doesn't have form data example but it allows that if you need to optional message_document file.

 curl --request POST 
  --url https://api-m.sandbox.paypal.com/v1/customer/disputes/12345/send-message` 
  --header 'authorization: Bearer --token--' 
  --header 'content-type: multipart/form-data' 
  -F "input={"message":"sample message"};type=application/json" 
  -F "[email protected]"

I tried that Node.js code even without sending file but not successful:

  const FormData = require('form-data');
  const form = new FormData();

  form.append('input', JSON.stringify({ message: 'sample message' }), { contentType: 'application/json' });

  const result = await axios({
    url: 'https://api-m.sandbox.paypal.com/v1/customer/disputes/12345/send-message',
    method: 'post',
    data: form,
    headers: {
      'Content-Type': 'multipart/form-data',
      Authorization: 'Bearer --token--'
    }
  }).catch(err => {
    throw new Error(err);
  });

I can't see that where is the problem. Please enlighten me. Thanks in advance.

question from:https://stackoverflow.com/questions/65875621/convert-paypal-curl-command-to-axios-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wasn't expecting but it is solved with missing boundary information. These can be used:

 const result = await axios({
    url: 'https://api-m.sandbox.paypal.com/v1/customer/disputes/12345/send-message',
    method: 'post',
    data: form,
    headers: {
      'Content-Type': `multipart/form-data; boundary=${form._boundary}`,
      Authorization: 'Bearer --token--'
    }
  })

or

 const result = await axios({
    url: 'https://api-m.sandbox.paypal.com/v1/customer/disputes/12345/send-message',
    method: 'post',
    data: form,
    headers: {
      Authorization: 'Bearer --token--',
      ...form.getHeaders()
    }
  })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...