检查 key:value 对是否存在于 POST 中
Check if key:value pair exist in POST
我正在尝试检查 POST 中是否定义了键和值,我想检查参数是否存在。我在 iOS 中这样称呼它,并不是说我如何制作 post
真的很重要
NSString *param = @"hello world";
NSString *postString = [NSString stringWithFormat:@"param=%@",param];
NSString *urlString = @"URL";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
//...
然后我有一个 php 脚本 return 与参数相同的值,但现在我想在 Python 中做同样的事情,所以我会去我的 python 脚本所在的 url 然后它将检查参数是否有值,如果有 return 相同的值。那么如何在 python 中执行此操作?
这里是php脚本
<?php
$response=array();
if(isset($_POST['param'])){
$response['success'] = true;
$response['message'] = 'received param = '.$_POST['param'];
}else{
$response['success'] = false;
$response['message'] = 'did not receive param';
}
$json = json_enconde($response);
感谢您的帮助
以下为纯Python。我建议你去 Django 在 Web 域中使用 Python。
import cgi
import json
form = cgi.FieldStorage()
param = form.getvalue("param")
if param:
json.dumps({'success': True, 'message': param })
else:
json.dumps({'success': False, 'message': 'did not receive parameter' })
我正在尝试检查 POST 中是否定义了键和值,我想检查参数是否存在。我在 iOS 中这样称呼它,并不是说我如何制作 post
真的很重要NSString *param = @"hello world";
NSString *postString = [NSString stringWithFormat:@"param=%@",param];
NSString *urlString = @"URL";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
//...
然后我有一个 php 脚本 return 与参数相同的值,但现在我想在 Python 中做同样的事情,所以我会去我的 python 脚本所在的 url 然后它将检查参数是否有值,如果有 return 相同的值。那么如何在 python 中执行此操作?
这里是php脚本
<?php
$response=array();
if(isset($_POST['param'])){
$response['success'] = true;
$response['message'] = 'received param = '.$_POST['param'];
}else{
$response['success'] = false;
$response['message'] = 'did not receive param';
}
$json = json_enconde($response);
感谢您的帮助
以下为纯Python。我建议你去 Django 在 Web 域中使用 Python。
import cgi
import json
form = cgi.FieldStorage()
param = form.getvalue("param")
if param:
json.dumps({'success': True, 'message': param })
else:
json.dumps({'success': False, 'message': 'did not receive parameter' })