Hello everyone, you need to automate filling out applications for 5 similar services, made the necessary value through the bash script, and how to substitute them into the request body, I will give a small clipping

--data-binary $'------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="sessid"\r\n\r\nc72598fa151fe6ddaae89efe476270bb\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="Update"\r\n\r\nY\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="COPY_ID"\r\n\r\n0\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="ACTIVE"\r\n\r\nY\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="NAME"\r\n\r\nTEST_NAME\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="LAST_NAME"\r\n\r\nTEST_FAMILY\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="SECOND_NAME"\r\n\r\nTEST_PATROM\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="EMAIL"\r\n\r\nTEST_EMAIL@testmail.ru\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="LOGIN"\r\n\r\nTest\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="NEW_PASSWORD"\r\n\r\nTEST_PASS\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="NEW_PASSWORD_CONFIRM"\r\n\r\nTEST_PASS\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="LID"\r\n\r\ncp\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="GROUP_ID_0"\r\n\r\n131\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="GROUP_ID_FROM_0"\r\n\r\n\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="GROUP_ID_TO_0"\r\n\r\n\r\n------WebKitFormBoundary358DzhBEThSvxSfb\r\nContent-Disposition: form-data; name="GROUP_ID_1"\r\n\r\n130\r\n------WebKitFormBoundary358DzhBE 

I want to draw attention to

 name="NAME"\r\n\r\nTEST_NAME\r\n------WebKitFormBoundary358DzhBEThSvxSfb`\r\nContent-Disposition: form-data; 

If I specify TEST_NAME, then everything works perfectly, if I substitute the $ User_Name variable with the same value, then it just does not work, this is understandable, because the query is in single quotes if you write:

 name="NAME"\r\n\r\n'$User_Name'\r\n------WebKitFormBoundary358DzhBEThSvxSfb`\r\nContent-Disposition: form-data; 

All that is after that will be cut off, and only this field will be inserted.

How to fix it?

  • --data-binary $' - why is there a dollar sign? - aleksandr barakin
  • He was here when he copied curl - Sergey Krylov
  • one
    let us suppose. then the second part of the line also makes sense to write it in this form. to get: --data-binary $'...'$User_Name$'...' - aleksandr barakin
  • Thank you so much) they helped out very much - Sergey Krylov

1 answer 1

The parameter passed to the curl program uses a special syntax for the string: $'строка' . It is supported in popular shells (for example, bash and zsh ), but as far as I know, it is not included in the posix standard.

inside such a string, the strings of the \n type are replaced with characters. for example, the string \n converted to a newline character with a hexadecimal value of 0a ).


I will show with an example that you "went wrong."

suppose we have a string containing \n . when interpreted by the shell, transformations occur:

 $ echo $'a\nb\nc' a b c 

if you do not use this syntax, then no conversion occurs:

 $ echo 'a\nb\nc' a\nb\nc 

breaking the line into two parts and substituting, for example, the character b output of the variable, we get:

 $ var=zzz; echo $'a\n'$var'\nc' a zzz\nc 

i.e., in the second part ( '\nc' ), the described transformations were not performed. so that they happen there, the second part must also be written using the same special syntax ( $'\nc' ):

 $ var=zzz; echo $'a\n'$var$'\nc' a zzz c