'Sending cURL requests from php to asp.net not working
I've been trying to fix this piece of code but it seems broken. If I use http_build_query($data) and just insert it into the $url at the end, it will work but if I do it the proper way (by using the curl_setopt(), it won't work.
$data = [
'target' => 'CupidonSauce173',
'isDisplayed' => true,
'langKey' => 'keyTest',
'varKeys' => "key1|u,key2|t",
'event' => 'eventTest',
];
$payload = http_build_query($data);
$url = "https://localhost:7020/api/notification/add";
$ch = curl_init($url);
# cURL Request setup.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
# Certification issues / debug only.
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$results = curl_exec($ch);
var_dump(curl_getinfo($ch));
var_dump(curl_errno($ch));
var_dump(curl_error($ch));
var_dump($results);
var_dump($payload);
This is the return from the var_dumps.
array(37) { ["url"]=> string(43) "https://localhost:7020/api/notification/add" ["content_type"]=> NULL ["http_code"]=> int(404) ["header_size"]=> int(88) ["request_size"]=> int(141) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(18) ["redirect_count"]=> int(0) ["total_time"]=> float(0.044471) ["namelookup_time"]=> float(0.000718) ["connect_time"]=> float(0.024922) ["pretransfer_time"]=> float(0.034063) ["size_upload"]=> float(96) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(2181) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(96) ["starttransfer_time"]=> float(0.034069) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(3) "::1" ["certinfo"]=> array(0) { } ["primary_port"]=> int(7020) ["local_ip"]=> string(3) "::1" ["local_port"]=> int(60377) ["http_version"]=> int(3) ["protocol"]=> int(2) ["ssl_verifyresult"]=> int(0) ["scheme"]=> string(5) "HTTPS" ["appconnect_time_us"]=> int(33956) ["connect_time_us"]=> int(24922) ["namelookup_time_us"]=> int(718) ["pretransfer_time_us"]=> int(34063) ["redirect_time_us"]=> int(0) ["starttransfer_time_us"]=> int(34069) ["total_time_us"]=> int(44471) } int(0) string(0) "" string(0) "" string(96) "target=CupidonSauce173&isDisplayed=1&langKey=keyTest&varKeys=key1%7Cu%2Ckey2%7Ct&event=eventTest"
The web API is an ASP.NET 6 program, here is the function "add".
// POST: Create a new notification in the distributed cache
[Route("api/[controller]/[action]/target={target}&isDisplayed={isDisplayed}&langKey={langKey}&varKeys={varKeys}&event={notifyEvent}")]
public async Task Add(string target, bool isDisplayed, string langKey, string varKeys, string notifyEvent)
{
List<string> keyList = new();
string[] keyStrings = varKeys.Split(',');
foreach(string key in keyStrings)
{
keyList.Add(key);
}
Notification notification = new(isDisplayed, target, langKey, keyList, notifyEvent);
// Set the notification in the distributed cache.
string serialized = JsonSerializer.Serialize(notification);
await distributedCache.SetStringAsync(notification.Id, serialized);
Debug.WriteLine(notification.Id);
}
I really have no idea why it doesn't work. Thanks for the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
