开发者社区> 问答> 正文

OSS PostObject用Erlang写了个测试,一直调不通,报MalformedPOSTRequest

%%osspostobj.erl
-module(osspostobj).

-export([test/0]).

test()->

inets:start(),  
%ssl:start(), 

Method = post,
URL = "http://huabei2-ttccmj.oss-cn-beijing.aliyuncs.com",



Header = [{"User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"}],
Type = "multipart/form-data; boundary=9431149156168",




Body = "--9431149156168\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\n1.json\r\n--9431149156168\r\nContent-Disposition: form-data; name=\"file\"; filename=\"1.json\"\r\nContent-Type: text/plain\r\n\r\n{\"animals\":{\"key1\":2,\"key2\":\"ab\"}}\r\n--9431149156168",

BodyBin = list_to_binary(Body),


HTTPOptions = [{timeout, 30000},{connect_timeout, 5000}],
Options = [{sync, true}],


case httpc:request(Method, {URL, Header, Type, Body}, HTTPOptions, Options) of
    {ok, {StatusLine,Headers,RBody}}-> 
        io:format("receive StatusLine body:~p ~p ~p~n", [StatusLine, Headers, RBody]);
    {ok, {saved_as, FilePath}}->
        io:format("receive saved_as FilePath:~p~n", [FilePath]);
    {ok, {Status, RBody}}->
        io:format("receive Status body:~p ~p~n", [Status,RBody]);
    {ok, RequestId}->
        io:format("receive RequestId:~p~n", [RequestId]);
    {error, Reason}->
        io:format("error cause ~p~n",[Reason])  
end.

展开
收起
郑心 2017-03-15 04:05:19 4212 0
1 条回答
写回答
取消 提交回答
  • 出现该报错,应该是Post请求的body格式非法,正确的写法如下:
    Body由一系列的表单域构成,表单域格式如下: Content-Disposition: form-data; name="{key}"rnrn{value}rn--{boundary}
    具体写法可查看:https://help.aliyun.com/document_detail/31988.html

    2019-07-17 20:55:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载