curl 为什么我的PHP post函数在NodeJS Express中接收时是这样的格式?

t8e9dugd  于 2024-01-08  发布在  PHP
关注(0)|答案(1)|浏览(207)

伙计们,我对PHP很陌生,有一个函数试图将数据发布到我的后端NodeJS Express API端点。
收到的req.body在我收到的时候变成了这样。
它追加了额外的**{ jsonObj:“"}**

  1. {{"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":{"name":"Steven Zhou","phone":"7788889128","email":"[email protected]"},"teamAdvisors":"TBD","addresses":{"street":"11451 4th ave","province":"BC","country":"Canada","zipcode":"V7E3H1"},"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}: ""}

字符串
我希望它是这样的格式

  1. {"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":{"name":"Steven Zhou","phone":"7788889128","email":"[email protected]"},"teamAdvisors":"TBD","addresses":{"street":"11451 4th ave","province":"BC","country":"Canada","zipcode":"V7E3H1"},"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}


下面是发送POST请求的PHP代码

  1. $data = [
  2. 'name' => 'PHP Game test',
  3. 'email' => '[email protected]',
  4. 'logo' => 'logo.jpg',
  5. 'heroImage' => 'hero.jpg',
  6. 'teamName' => 'test',
  7. 'teamSize' => '4',
  8. "type" => "publisher",
  9. "mainContacts"=> [
  10. "name"=> "Steven Zhou",
  11. "phone"=> "7788889128",
  12. "email"=> "[email protected]"
  13. ],
  14. "teamAdvisors"=> "TBD",
  15. "addresses"=> [
  16. "street"=> "11451 4th ave",
  17. "province"=> "BC",
  18. "country"=> "Canada",
  19. "zipcode"=> "V7E3H1"
  20. ],
  21. "url"=> "appcodes.io",
  22. "officeLocations"=> "5",
  23. "gamesProduced"=> "2",
  24. "gamesInDevelopment"=> "3",
  25. "description"=> "Test API"
  26. ];
  27. $jsonData = json_encode($data, JSON_FORCE_OBJECT);
  28. curl_setopt_array($curl, array(
  29. CURLOPT_URL => "http://localhost:3006/api/company",
  30. CURLOPT_CUSTOMREQUEST => "POST",
  31. CURLOPT_POSTFIELDS => $jsonData,
  32. CURLOPT_HTTPHEADER => array(
  33. "contentType : application/json",
  34. "Accept: */*",
  35. "Connection: keep-alive",
  36. "Access-Control-Allow-Origin:*",
  37. "Accept-Encoding: gzip, deflate, br",
  38. "api-key: ..."
  39. ),
  40. ));
  41. $response = curl_exec($curl);
  42. echo $response;
  43. $err = curl_error($curl);
  44. curl_close($curl);


当我不使用json_encode函数时,它会正确发送数据,除非地址和联系人变成了没有键的普通数组,如下图所示。

  1. {"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":["Steven Zhou","7788889128","[email protected]"],"teamAdvisors":"TBD","addresses":["11451 4th ave","BC","Canada","V7E3H1"],"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}


下面是接收请求的NodeJS Express端点

  1. router.post('/company', multer.none(), verifyWordpressApiKey, createCompany);
  2. async function createCompany ( req, res, next){
  3. try{
  4. if(req.body.logo.length === 0) {
  5. return res.status(400).send({errorMessage: 'No company logo uploaded.'});
  6. } else if(req.body.heroImage.length === 0) {
  7. return res.status(400).send({errorMessage: 'No hero image uploaded.'});
  8. }
  9. req.body.createdBy = DB_WORDPRESS_USER;
  10. let jsonObj = JSON.parse(req.body.mainContacts);
  11. const organizationResult = await createOrganizationFromPipedrive(req.body.name);
  12. await createNewPersonFromPipedrive(organizationResult.id,
  13. jsonObj[0],
  14. jsonObj[1],
  15. jsonObj[2]);
  16. await createInternalCompany(req.body);
  17. res.sendStatus(200);
  18. }catch (error) {
  19. next(new Error('Unable to create a new company' + error));
  20. }
  21. }


我很困惑,任何帮助都很感激。
谢谢你,谢谢

6xfqseft

6xfqseft1#

CURL请求中将contentType更改为Content-Type
并确保在Express中使用

  1. app.use(express.json());// this will format all incoming request as JSON

字符串

相关问题