curl命令来测试API [英] Curl command to test the API

查看:177
本文介绍了curl命令来测试API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试以下规格的API

 网​​址:http://myapp.com/api/upload/
方法:POST
参数:
   图像(图像的二进制数据)回报
   JSON对象

样PHP执行测试API

 < PHP
$ CH = curl_init();
$ URL ='http://myapp.com/api/upload/';$栏=阵列('形象'=>的file_get_contents('轮廓-pic.jpg'));curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_POSTFIELDS,$领域);
curl_setopt($ CH,CURLOPT_URL,$网址);$ JSON = curl_exec($ CH);
回声\\ n$ json的\\ n。?>

我使用RESTClient实现和海报扩展测试其他的API,我不知道如何发送二进制数据为命名的图像与那些工具的参数。

我如何使用curl命令测试上面的API?

没有运气这一次

 卷曲-X POST --data二进制image=@test.jpghttp://myapp.com/api/upload/

test.jpg放在是当前目录。我无法改变的第三方API,能够与多种编码格式进行测试。


解决方案

 卷曲-X POST --data-urlen code'image@test.jpg的http:/ /myapp.com/api/upload

这是图像@ ,而不是图像= @

这是假设的图像数据发送,这是我的身影了PHP code不前urlen codeD。

I need to test an API of following spec

URL: http://myapp.com/api/upload/
Method: POST
Parameters: 
   image: (binary data of image)

returns
   a JSON Object

sample php implementation to test the api

<?php
$ch = curl_init();
$url = 'http://myapp.com/api/upload/';

$fields = array('image'=>file_get_contents('profile-pic.jpg'));

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);

$json = curl_exec($ch);
echo "\n".$json."\n";

?>

I use RESTClient and Poster extensions to test other api, I'm not sure how to send binary data for a parameter named image with those tools.

How do I test the above api using Curl command?

No luck with this one

curl -X POST --data-binary "image=@test.jpg" "http://myapp.com/api/upload/"

test.jpg is in current directory. I can't change the 3rd party API, to be able to test with other encoding formats.

解决方案

curl -X POST --data-urlencode 'image@test.jpg' 'http://myapp.com/api/upload'

It’s image@, not image=@.

This is assuming that the image data is urlencoded before sending, which is what I figure the PHP code does.

这篇关于curl命令来测试API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆