Rest Call using winhttp



  • Help to get and display text from rest endpoint , tried using winhttp but no luck.



  • @Vivek-Ghule

    Hmm, I think your answer is HERE.



  • @Alan-Kilborn
    thanks, I think its related to N++ plugin dev so just posted here, no problem will move it to help–needed.



  • @Vivek-Ghule said in Rest Call using winhttp:

    thanks, I think its related to N++ plugin dev so just posted here, no problem will move it to help–needed.

    If you think it’s related to a Notepad++ plugin development, you need to explain how. Your original post didn’t even mention Notepad++, let alone that you were developing a plugin for Notepad++.

    Moreover, while the Plugin Development section of the forum is for help with developing plugins, it’s mostly focused on the aspects of the plugin development that are specific to Notepad++. So, if you are having trouble using the Win32 WinHTTP library in general, this might not be the best place to ask. But if you have an example of “WinHTTP works for me in this short standalone example code, but the same syntax for WinHTTP doesn’t work when I am embedding that code inside a Notepad++ plugin”, then someone might be able to help you. But you’re going to have to give us more than one or two sentences.

    As it stands, it appears to me that you are asking for general help with using WinHTTP, rather than help with making a Notepad++ plugin. If you disagree, give more details as to what you are trying to accomplish, and why you think it is Notepad++-specific.

    Thus, @Alan-Kilborn’s assertion that you posted in the wrong place wasn’t meant to say, “use “help wanted” rather than “plugin development”.” It was meant to say, “this isn’t a generic coding forum, so if you have a generic coding quesion, then the forum at community.notepad-plus-plus.org isn’t the right place”

    Regarding using REST calls inside a Notepad++ plugin: it’s definitely doable. In a post from last month, @eljefe7000 posted a link to their RestApiToText plugin, which makes a REST call based on the text you have in the active tab, and pastes the result into Notepad++. Since the code is on github, you may be able to take inspiration from how @eljefe7000 implemented the REST calls in the plugin.



  • @PeterJones
    Thanks a lot for response, I am sorry actually i am new to this community. I also realized regarding my query that is very abstract and not clear.

    My goal is to periodically hit the Rest Endpoint and display the returned text to Notepad++ document. so i searched if there is any plugin related to this I found one but its not working as expected. so I decided to give a try to develop.
    initially I tried with some c++ wrapper for curl but its not supported by notepadd++ , so I tried using winhttp I was trying example (https://docs.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpconnect) but npp gets closed when i try to run my plugin. so i am looking for some help in this regards.



  • @Vivek-Ghule said in Rest Call using winhttp:

    i searched if there is any plugin related to this I found one but its not working as expected. so I decided to give a try to develop.

    I like the go-getter attitude.

    I cannot help you with how to use WinHTTP.

    But if I were developing a plugin and had a question, I would narrow it down to the smallest possible code that replicated the problem, and then when asking the question, provide a link to a github repo that had my example code that I was trying to debug.

    The problem with starting as a plugin is that you don’t know whether the crash is coming because you’ve done something wrong in the plugin interface, or because you’ve made some generic error in the generic c++ code (ie, with calling WinHTTP). So maybe even before showing a github repo link, I would recommend making a very simple small .exe that has the simplest code you can come up with: load the WinHTTP library, make the connect call, and exit. See whether that crashes or works. You could then slowly build it up until the generic c++ program is able to correctly GET a response from your REST server. If you have trouble with that code, a generic coding site that knows Win32 and C++ (maybe stackoverflow) would be the best bet. Once you have the simple example working in exe form, then try adding it to a working Notepad++ Plugin template. If you get to the point where the generic plugin template compiles and doesn’t crash Notepad++, but the minimal executable still works, and the WinHTTP-specific code is identical between the standalone code and the plugin code, then I would suggest putting both program’s c++ source code in the github, and point us to that github, and see if anyone here is able to help you.



  • @PeterJones said in Rest Call using winhttp:

    i searched if there is any plugin related to this I found one but its not working as expected.

    BTW: you didn’t say for sure whether the “found one” was the RestApiToText plugin, or some other plugin. Could you tell us which plugin?

    If it is the RestApiToText plugin, you can see in the RestApiToText plugin announcement that we’re having difficulty getting @eljefe7000’s plugin to work as they described it. If you can confirm that also, and if you were willing to follow my suggestion in that other thread and post a bug report issue for RestApiToText, we might find out how to get that plugin working correctly, and then you wouldn’t have to write your own.



  • @PeterJones
    Thanks for suggestion Finally I can now able to send get request and show received data to notepad++ document, but last few lines truncated while displaying. the same code prints complete received response in console .

    winInet works fine instead of winhttp below is code snippet which hits rest endpoint and display the data.

    void getData()
    {
    
    	// Open a new document
    	::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);
    
    	// Get the current scintilla
    	int which = -1;
    	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    	if (which == -1)
    		return;
    	HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
    	
    	HINTERNET hSession = InternetOpen(
    		L"Mozilla/5.0",
    		INTERNET_OPEN_TYPE_PRECONFIG,
    		NULL,
    		NULL,
    		0);
    
    	HINTERNET hConnect = InternetConnect(
    		hSession,
    		L"jsonplaceholder.typicode.com",
    		INTERNET_DEFAULT_HTTPS_PORT, // THIS
    		L"",
    		L"",
    		INTERNET_SERVICE_HTTP,
    		0,
    		0);
    
    	HINTERNET hHttpFile = HttpOpenRequest(
    		hConnect,
    		L"GET",
    		L"/todos",
    		NULL,
    		NULL,
    		NULL,
    		INTERNET_FLAG_SECURE, // THIS
    		0);
    
    	while (!HttpSendRequest(hHttpFile, NULL, 0, 0, 0)) {
    		//std::cout << "HttpSendRequest error : " << GetLastError();
    		::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"HttpSendRequest error :");
    
    		InternetErrorDlg(
    			GetDesktopWindow(),
    			hHttpFile,
    			ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED,
    			FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
    			FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
    			FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
    			NULL);
    	}
    
    	DWORD dwFileSize;
    	dwFileSize = 128;
    	std::string response;
    
    	char* buffer;
    	buffer = new char[dwFileSize + 1];
    	memset(buffer, 0x00, sizeof(buffer));
    
    	while (true) {
    		DWORD dwBytesRead;
    		BOOL bRead;
    
    		std::string strBuffer(buffer);
    
    		bRead = InternetReadFile(
    			hHttpFile,
    			buffer,
    			dwFileSize + 1,
    			&dwBytesRead);
    
    		if (dwBytesRead == 0) break;
    
    		if (!bRead) {
    			//std::cout << "InternetReadFile error : : " << GetLastError();
    			::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"InternetReadFile error:");
    		}
    		else {
    			buffer[dwBytesRead] = 0;
    			response += strBuffer;
    			::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)response.c_str());
    		}
    	}
    
    	InternetCloseHandle(hHttpFile);
    	InternetCloseHandle(hConnect);
    	InternetCloseHandle(hSession);
    
    }
    
    attached the responses from npp and exe.![npp1.PNG](/assets/uploads/files/1607162185969-npp1.png) ![expected.PNG](/assets/uploads/files/1607162185934-expected.png) 
    


  • @PeterJones
    yes the only plugin which i found is “RestApiToText” , as suggested will post bug.



  • @Vivek-Ghule said in Rest Call using winhttp:

    1607162185969

    npp1.PNG expected.PNG



  • @Vivek-Ghule ,

    1607162185969

    This post was a bit cryptic. I don’t know what that number is meant to indicate.

    Do the screenshots mean that you got the RestApiToText plugin to work? If so, how?



  • @PeterJones
    I mean I have write some code to display text from rest endpoint and it worked. the screenshots are one with exe console and other NPP which truncate the last few line. Anyways I found root cause on this will try to fix it. Thanks a lot for your kind attention.


Log in to reply