Home > Enterprise >  How to send inline xml in curl with multipart/form-data
How to send inline xml in curl with multipart/form-data

Time:05-02

I need to send some inline xml as a value in multipart/form-data request with curl. To be more precise, Im using ansible shell to run this curl. I have a problem that when request is send I got an error, that some client-certificate is not found.

couldn't open file "?xml version=1.0 encoding=UTF-8?>
<eventPublisher name=EmailPublisher processing=enable
statistics=disable trace=disable 
xmlns=http://wso2.org/carbon/eventpublisher>
<from streamName=id_gov_notify_stream version=1.0.0/>
<mapping

XML im trying to send:

<?xml version="1.0" encoding="UTF-8"?>
<eventPublisher name="EmailPublisher" processing="enable"
  statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventpublisher">
  <from streamName="id_gov_notify_stream" version="1.0.0"/>
  <mapping customMapping="enable" type="text">
    <inline>{{body}}</inline>
  </mapping>
  <to eventAdapterType="email">
    <property name="email.subject">{{subject}}</property>
    <property name="email.address">{{send-to}}</property>
    <property name="email.type">{{content-type}}</property>
  </to>
</eventPublisher>

curl request in Ansible:

'curl --location --request POST https://*** --header "Accept: 
 application/json" --header "Content-Type: multipart/form-data" -- 
 user admin@{{ domain}}@{{domain}}:{{ pass }} -F "resourceFile= 
 {{lookup("file", "file.xml")}}" -F "fileName=EmailPublisher"'

Somehow first character from resourceFile key is removed. How to escape or fix it that < will be send with the request?

BTW Im using curl because form-data is supported in uri module since ansible 2.10 and Im using 2.9 version.

CodePudding user response:

It's not that "the first character was removed," it's that -F alpha=<filename is how curl interprets "read the form data field alpha from the filename" after < just like in shell redirection.

You likely want to use --form-string because it does not make the leading < magic

You'll also want to either use ' as the shell quote, or ask ansible to quote that XML for you, because the way you had it was -F x="<?xml version="1.0" encoding="UTF-8"?>" which as one can see is not legal shell

--form-string resourceFile={{ lookup("file", "file.xml") | quote }}

or

--form-string 'resourceFile={{ lookup("file", "file.xml") }}' if you're 100% positive the file can never contain a ' by itself. Obviously the first form is safer because it makes no such assumptions

  • Related