How to format a file submission Matlab

248 views
Skip to first unread message

JP

unread,
Apr 23, 2017, 9:54:32 AM4/23/17
to astrometry
I am writing a script in Matlab to connect to the API service and solve for a table of x/y pixel positions.

To get a session, we have the following in Matlab:

-
options = weboptions('MediaType','application/x-www-form-urlencoded','RequestMethod','post');
apikey = 'XXXXXXXX';
json_command = ['request-json={"apikey": "', apikey, '"}'];
response = webwrite(url,json_command,options);
-


When using a correct apikey, then it gives the result:

response = {"status": "success", "message": "authenticated user: xxx...@gmail.com", "session": "ar4x5e3yt2ktgpqmgsbbshkjwemcz8n9"}


So far so good.  Now I need to make the argument list and the character string to send the data:


-
session = 'ar4x5e3yt2ktgpqmgsbbshkjwemcz8n9';
scale_est = 0.416;
scale_err = 2;
center_ra = 180.1083432;
center_dec = -45.5434345;
radius = 0.3;
tweak_order = 0;
parity = 2;
image_width = 4096;
image_height = 4096;
positional_error = 1;

json_command = [];
json_command = ['{"session": "', session,'", '];
json_command = [json_command, '"url": "', url,'", '];
json_command = [json_command, '"allow_commercial_use": ', '"n','", '];
json_command = [json_command, '"allow_modifications": ', '"n','", '];
json_command = [json_command, '"publicly_visible": ', '"n','", '];
json_command = [json_command, '"scale_units": ', '"arcsecperpix','", '];
json_command = [json_command, '"scale_type": ', '"ev','", '];
json_command = [json_command, '"scale_est": ', num2str(scale_est,4),', '];
json_command = [json_command, '"scale_err": ', num2str(scale_err,3),', '];
json_command = [json_command, '"center_ra": ', num2str(center_ra,8),', '];
json_command = [json_command, '"center_dec": ', num2str(center_dec,8),', '];
json_command = [json_command, '"radius": ', num2str(radius,5),', '];
json_command = [json_command, '"tweak_order": ', num2str(tweak_order),', '];
json_command = [json_command, '"parity": ', num2str(parity),', '];
json_command = [json_command, '"image_width": ', num2str(image_width),', '];
json_command = [json_command, '"image_height": ', num2str(image_height),', '];
json_command = [json_command, '"positional_error": ', num2str(positional_error),', '];
json_command(end-1:end) = [];
json_command = [json_command, '}'];

uploadfilePOST = sprintf(['--===============2521702492343980833==\nContent-Type: text/plain\nMIME-Version: 1.0\nContent-disposition: form-data; name="request-json"\n\n' ,json_command]);
uploadfilePOST = sprintf([uploadfilePOST, '\n--===============2521702492343980833==\nContent-Type: application/octet-stream\nMIME-Version: 1.0\n']);
filename = 'test.txt';
uploadfilePOST = sprintf([uploadfilePOST, 'Content-disposition: form-data; name="file"; filename="', filename, '"\n']);
positions = round(rand(20,2)*4000,4);%dummy data positions
for i = 1: length(positions)
    uploadfilePOST = sprintf([uploadfilePOST, '\n', num2str(positions(i,1)), ' ', num2str(positions(i,2))]);
end
uploadfilePOST = sprintf([uploadfilePOST, '\n\n--===============2521702492343980833==--']);
-

This gives: for the uploadfilePOST string:



--===============2521702492343980833==
Content-Type: text/plain
MIME-Version: 1.0
Content-disposition: form-data; name="request-json"

{"session": "ar4x5e3yt2ktgpqmgsbbshkjwemcz8n9", "url": "http://nova.astrometry.net/api/upload", "allow_commercial_use": "n", "allow_modifications": "n", "publicly_visible": "n", "scale_units": "arcsecperpix", "scale_type": "ev", "scale_est": 0.416, "scale_err": 2, "center_ra": 180.10834, "center_dec": -45.543434, "radius": 0.3, "tweak_order": 0, "parity": 2, "image_width": 4096, "image_height": 4096, "positional_error": 1}
--===============2521702492343980833==
Content-Type: application/octet-stream
MIME-Version: 1.0
Content-disposition: form-data; name="file"; filename="test.txt"

2577.2725 1244.4091
1514.4375 3693.5186
3246.3218 1720.8296
2131.3024 739.2653
1402.9084 3619.5239
3756.0062 3918.9935
3503.7712 1755.4799
2200.6254 444.4769
2489.9003 1032.2588
2348.1788 1634.8794
830.9692 2379.5843
1204.9853 1048.847
1883.6934 2411.3724
921.9526 2844.8631
3377.2352 886.9869
779.0572 469.6706
903.6871 1186.7035
682.8322 1275.1132
910.6572 1696.667
1742.7947 2031.4331

--===============2521702492343980833==--


Then, when I send:

-
response = webwrite(url,uploadfilePOST,options);
-

the response is:

{"status": "error", "errormessage": "no json"}


So, some error is occurring.  It says the error is with the json sent to it, but the json formatted argument list looks OK to me.

First question is: Are you actually supposed to send all that text with the ========2521702492343980833== etc.?
Another question:  Should I actually be setting something more in the Matlab "weboptions" for the webwrite command as was done to get the session?  That is, something additional to:  

options = weboptions('MediaType','application/x-www-form-urlencoded','RequestMethod','post');?

Matlab Help on weboptions is here if you wish to read it:  https://www.mathworks.com/help/matlab/ref/weboptions.html

Thanks for any pointers and help.

Dustin Lang

unread,
Apr 23, 2017, 10:54:20 AM4/23/17
to astrometry
Hi,

You should almost certainly NOT be formatting a string with the =====252170249... stuff in it.  That is a MIME multipart message (and that numbered string must match a value in the HTML request headers anyway).  I have not touched Matlab in over a decade, and am happy for that, but you should look into whether it has a way of sending a multipart message.  For the record, this is *exactly* how a regular HTML form including a text box and a file upload looks.  At a concrete level, the media type is not application/x-www-form-urlencoded, it is multipart/form-data.

cheers,
--dustin
Reply all
Reply to author
Forward
0 new messages